package com.alibaba.citrus.cr.unified.inventory.functions.allocateOrder;

import java.util.Date;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

import com.alibaba.citrus.cr.unified.inventory.repository.AllocateOrderReadRepository;
import com.alibaba.citrus.cr.unified.inventory.repository.AllocateOrderWriteRepository;
import com.alibaba.citrus.cr.common.util.JsonUtil;
import com.alibaba.citrus.cr.common.util.SpringContextUtil;
import com.alibaba.citrus.cr.common.util.TimeUtil;
import com.alibaba.citrus.cr.unified.inventory.functions.allocateOrderDetail.AllocateOrderDetailEntity;
import com.alibaba.citrus.cr.unified.inventory.functions.allocateOrderDetail.AllocateOrderDetailFactory;

import com.epoch.app.crunifiedinventory.dto.request.AllocateOrderSyncRequest;
import com.epoch.app.crunifiedinventory.model.dto.AllocateOrderCreateRequest;
import com.epoch.app.crunifiedinventory.sdo.AllocateOrderSDO;
import lombok.Data;
import org.apache.commons.lang3.StringUtils;

@Data
public class AllocateOrderEntity {

    private AllocateOrderSDO allocateOrderSDO;
    private List<AllocateOrderDetailEntity> allocateOrderDetailEntities;
//private List<XXXSDO>
//    List<XXXdetailEntoity>
    private static AllocateOrderReadRepository allocateOrderReadRepository;
    private static AllocateOrderWriteRepository allocateOrderWriteRepository;
    static {
        allocateOrderReadRepository = SpringContextUtil.getBean(AllocateOrderReadRepository.class);
        allocateOrderWriteRepository = SpringContextUtil.getBean(AllocateOrderWriteRepository.class);
    }
    public static AllocateOrderEntity of(AllocateOrderSDO allocateOrderSDO) {
        AllocateOrderEntity entity = new AllocateOrderEntity();
        entity.setAllocateOrderSDO(allocateOrderSDO);
        return entity;
    }

    public static AllocateOrderEntity of(AllocateOrderCreateRequest allocateOrderCreateRequest){
        AllocateOrderSDO allocateOrderSDO = convertAllocateOrderSDO(allocateOrderCreateRequest);
        AllocateOrderEntity allocateOrderEntity = of(allocateOrderSDO);
        List<AllocateOrderDetailEntity> allocateOrderDetailEntities =
                AllocateOrderDetailFactory.create(allocateOrderCreateRequest);
        allocateOrderEntity.setAllocateOrderDetailEntities(allocateOrderDetailEntities);
        return allocateOrderEntity;
    }

    /**
     * 根据F-F1 sap->ots 创建/更新调拨单
     * @param allocateOrderSyncRequest
     * @return
     */
    public static AllocateOrderEntity of(AllocateOrderSyncRequest allocateOrderSyncRequest){
        AllocateOrderSDO allocateOrderSDO = convertAllocateOrderSDO(allocateOrderSyncRequest);
        AllocateOrderEntity allocateOrderEntity = of(allocateOrderSDO);
        List<AllocateOrderDetailEntity> allocateOrderDetailEntities =
                AllocateOrderDetailFactory.create(allocateOrderSyncRequest);
        allocateOrderEntity.setAllocateOrderDetailEntities(allocateOrderDetailEntities);
        return allocateOrderEntity;
    }

    public static AllocateOrderSDO convertAllocateOrderSDO(AllocateOrderCreateRequest allocateOrderSyncRequest){
        AllocateOrderSDO allocateOrderSDO = new AllocateOrderSDO();
        if (allocateOrderSyncRequest.getAllocateApplyOrderId() != null){
            allocateOrderSDO.setAllocateApplyOrderId(allocateOrderSyncRequest.getAllocateApplyOrderId());
        }
        allocateOrderSDO.setOutAllocateOrderId(allocateOrderSyncRequest.getOutAllocateApplyOrderId());
//        allocateOrderSDO.setApplyOrgId(Optional.ofNullable(allocateOrderSyncRequest.getApplyOrgId())
//                .map(Integer::parseInt)
//                .orElse(null));
        allocateOrderSDO.setApplyOrgId(allocateOrderSyncRequest.getApplyOrgId());
        allocateOrderSDO.setOutAllocateApplyOrderId(allocateOrderSyncRequest.getOutAllocateApplyOrderId());
        allocateOrderSDO.setCode(allocateOrderSyncRequest.getCode());
        allocateOrderSDO.setBeginDate(allocateOrderSyncRequest.getCreateTime());
        allocateOrderSDO.setCompleteTime(allocateOrderSyncRequest.getCompleteTime());
        //TODO 获取到调拨预报单 ? 确认数量 来自于 调拨货品明细里的数字吧?
//        allocateOrderSDO.setConfirmQuantity(allocateOrderSyncRequest.getConfirmTime());
        allocateOrderSDO.setCreateTime(new Date());
        allocateOrderSDO.setCustomer(allocateOrderSyncRequest.getCustomer());
        allocateOrderSDO.setDiscount(allocateOrderSyncRequest.getDiscount());
        allocateOrderSDO.setCreator(allocateOrderSyncRequest.getCreator());
        allocateOrderSDO.setConfirmTime(allocateOrderSyncRequest.getConfirmTime());
        allocateOrderSDO.setExpectedDeliveryDate(new Date());
//        allocateOrderSDO.setFeatures(allocateOrderSyncRequest.get);
//        allocateOrderSDO.setProductGroup(Optional.ofNullable(allocateOrderSyncRequest.getProductGroup())
//                .map(Integer::parseInt).orElse(null));
        allocateOrderSDO.setProductGroup(allocateOrderSyncRequest.getProductGroup());
        allocateOrderSDO.setType(allocateOrderSyncRequest.getType());
        allocateOrderSDO.setTotalQuantity(StringUtils.isBlank(allocateOrderSyncRequest.getTotalQuantity())?
                null:Long.parseLong(allocateOrderSyncRequest.getTotalQuantity()));
        allocateOrderSDO.setTotalAmount(allocateOrderSyncRequest.getTotalAmount());
        allocateOrderSDO.setStatus(Optional.ofNullable(allocateOrderSyncRequest.getStatus())
                .map(Integer::parseInt).orElse(null));
        allocateOrderSDO.setShipWarehouseId(allocateOrderSyncRequest.getShipWareHouseId());
        allocateOrderSDO.setShipMethod(allocateOrderSyncRequest.getShipMethod());
        allocateOrderSDO.setShipAreaId(allocateOrderSyncRequest.getShipAreaId());
        allocateOrderSDO.setSendBackRequired(allocateOrderSyncRequest.getSendBackRequired());
        allocateOrderSDO.setSapRefPoNo(allocateOrderSyncRequest.getSapRefPoNo());
        allocateOrderSDO.setSapCostCenterId(allocateOrderSyncRequest.getSapCostCenter());
        allocateOrderSDO.setSapChannel(allocateOrderSyncRequest.getSapChannel());
        allocateOrderSDO.setReturnOrder(allocateOrderSyncRequest.getReturnOrder());
        allocateOrderSDO.setRemark(allocateOrderSDO.getRemark());
        allocateOrderSDO.setReceiveWarehouseId(allocateOrderSyncRequest.getReceiveWareHouseId());
        allocateOrderSDO.setReceiveAreaId(allocateOrderSyncRequest.getReceiveAreaId());
        allocateOrderSDO.setConfirmQuantity(allocateOrderSyncRequest.getConfirmQuantity());
        allocateOrderSDO.setFounder(allocateOrderSyncRequest.getFounder());
        allocateOrderSDO.setDocDate(TimeUtil.parseSimpleDateFormat(allocateOrderSyncRequest.getDocDate()));
        allocateOrderSDO.setRemark(allocateOrderSyncRequest.getRemark());
        allocateOrderSDO.setLastUpdateUser(allocateOrderSyncRequest.getLastUpdateUser());
        allocateOrderSDO.setType(allocateOrderSyncRequest.getType());
        return allocateOrderSDO;
    }

    public static AllocateOrderSDO convertAllocateOrderSDO(AllocateOrderSyncRequest request){
        AllocateOrderSDO sdo = JsonUtil.convert(request,AllocateOrderSDO.class);
//        sdo.setCode(request.getCode());
//        sdo.setSapCostCenter(request.getSapCostCenter());
//        sdo.setOrderReason(request.getOrderReason());
//        sdo.setSapRefPoNo(request.getSapRefPoNo());
//        sdo.setTypeCard(request.getTypeCard());
//        sdo.setFounder(request.getFounder());
//        sdo.setConfirmQuantity(request.getConfirmQuantity());
//        sdo.setGmtCreate(request.getGmtCreate());


//        allocateOrderSDO.setOutAllocateOrderId(allocateOrderSyncRequest.getOutAllocateApplyOrderId());
//        allocateOrderSDO.setApplyOrgId(allocateOrderSyncRequest.getApplyOrgId());
//        allocateOrderSDO.setOutAllocateApplyOrderId(allocateOrderSyncRequest.getOutAllocateApplyOrderId());
//        allocateOrderSDO.setCode(allocateOrderSyncRequest.getCode());
//        allocateOrderSDO.setBeginDate(allocateOrderSyncRequest.getCreateTime());
//        allocateOrderSDO.setCompleteTime(allocateOrderSyncRequest.getCompleteTime());
//        //TODO 获取到调拨预报单 ? 确认数量 来自于 调拨货品明细里的数字吧?
////        allocateOrderSDO.setConfirmQuantity(allocateOrderSyncRequest.getConfirmTime());
//        allocateOrderSDO.setCreateTime(new Date());
//        allocateOrderSDO.setCustomer(allocateOrderSyncRequest.getCustomer());
//        allocateOrderSDO.setDiscount(allocateOrderSyncRequest.getDiscount());
//        allocateOrderSDO.setCreator(allocateOrderSyncRequest.getCreator());
//        allocateOrderSDO.setConfirmTime(allocateOrderSyncRequest.getConfirmTime());
//        allocateOrderSDO.setExpectedDeliveryDate(allocateOrderSyncRequest.getExpectedDeliveryDate());
////        allocateOrderSDO.setFeatures(allocateOrderSyncRequest.get);
//        allocateOrderSDO.setProductGroup(allocateOrderSyncRequest.getProductGroup());
//        allocateOrderSDO.setType(allocateOrderSyncRequest.getType());
//        allocateOrderSDO.setTotalQuantity(allocateOrderSyncRequest.getTotalQuantity());
//        allocateOrderSDO.setTotalAmount(allocateOrderSyncRequest.getTotalAmount());
//        allocateOrderSDO.setStatus(allocateOrderSyncRequest.getStatus());
//        allocateOrderSDO.setShipWarehouseId(allocateOrderSyncRequest.getShipWarehouseId());
//        allocateOrderSDO.setShipMethod(allocateOrderSyncRequest.getShipMethod());
//        allocateOrderSDO.setShipAreaId(allocateOrderSyncRequest.getShipAreaId());
//        allocateOrderSDO.setSendBackRequired(allocateOrderSyncRequest.getSendBackRequired());
//        allocateOrderSDO.setSapRefPoNo(allocateOrderSyncRequest.getSapRefPoNo());
//        allocateOrderSDO.setSapCostCenterId(allocateOrderSyncRequest.getSapCostCenterId());
//        allocateOrderSDO.setSapChannel(allocateOrderSyncRequest.getSapChannel());
//        allocateOrderSDO.setReturnOrder(allocateOrderSyncRequest.getReturnOrder());
//        allocateOrderSDO.setRemark(allocateOrderSDO.getRemark());
//        allocateOrderSDO.setReceiveWarehouseId(allocateOrderSyncRequest.getReceiveWarehouseId());
//        allocateOrderSDO.setReceiveAreaId(allocateOrderSyncRequest.getReceiveAreaId());
//        allocateOrderSDO.setConfirmQuantity(allocateOrderSyncRequest.getConfirmQuantity());
//        allocateOrderSDO.setFounder(allocateOrderSyncRequest.getFounder());
//        allocateOrderSDO.setDocDate(allocateOrderSyncRequest.getDocDate());
//        allocateOrderSDO.setRemark(allocateOrderSyncRequest.getRemark());
//        allocateOrderSDO.setLastUpdateUser(allocateOrderSyncRequest.getLastUpdateUser());
//        allocateOrderSDO.setType(allocateOrderSyncRequest.getType());
//        allocateOrderSDO.setOrderReason(allocateOrderSyncRequest.getOrderReason());
//        if (StringUtils.isNotBlank(allocateOrderSyncRequest.getTypeCard())){
//            allocateOrderSDO.setTypeCard(allocateOrderSyncRequest.getTypeCard());
//        }
//        allocateOrderSDO.setReturnOrder(allocateOrderSyncRequest.getReturnOrder());
//        allocateOrderSDO.setTypeCard(allocateOrderSyncRequest.getTypeCard());
        return sdo;
    }

    public static Integer getIntValue(String obj){
        return Optional.ofNullable(obj).map(Integer::parseInt).orElse(null);
    }

    public String salesOrderUpdate(){
        String allocateOrderId = allocateOrderWriteRepository.create(allocateOrderSDO);
        allocateOrderDetailEntities.forEach(x-> {
            x.getAllocateOrderDetailSDO().setAllocateOrderId(allocateOrderId);
            x.salesOrderUpdate();
        });
        return allocateOrderId;
    }

    public String create(){
        String allocateOrderId = allocateOrderWriteRepository.create(allocateOrderSDO);

        allocateOrderDetailEntities.forEach(x-> {
                    x.getAllocateOrderDetailSDO().setAllocateOrderId(allocateOrderId);
                    x.create();
                });
        return allocateOrderId;
    }

    public String sync(){
        String allocateOrderId = allocateOrderWriteRepository.sync(allocateOrderSDO,
                allocateOrderDetailEntities.stream().map(AllocateOrderDetailEntity::getAllocateOrderDetailSDO).collect(Collectors.toList()));
//        allocateOrderDetailEntities.forEach(x->{
//                    x.getAllocateOrderDetailSDO().setAllocateOrderId(Long.parseLong(allocateOrderId));
//                    x.getAllocateOrderDetailSDO().setAllocateOrderCode(allocateOrderSDO.getCode());
//                    x.sync();
//                    }
//                );
        return allocateOrderId;
    }

    public void doSomething() {
        // TODO: 2021/8/25
    }


}
